<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="交易所" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择交易所" clearable size="small">
          <el-option
            v-for="dict in platformOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="币对名称" prop="symbolName">
        <el-input
          v-model="queryParams.symbolName"
          placeholder="请输入币对名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="顺序" prop="sequence">
        <el-input
          v-model="queryParams.sequence"
          placeholder="请输入顺序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['platform:exchange:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="运行实例ID" align="center" prop="runningId"/>
      <el-table-column label="交易所" align="center" prop="platform"/>
      <el-table-column label="api_id" align="center" prop="apiId"/>
      <el-table-column label="币对名称" align="center" prop="symbolName"/>
      <el-table-column label="标签" align="center" prop="tagName"/>
      <el-table-column label="顺序" align="center" prop="sequence"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handleUpdate(scope.row)"-->
            <!--v-hasPermi="['platform:exchange:edit']"-->
          <!--&gt;修改-->
          <!--</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:exchange:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运行机器人币对对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="交易所" prop="platform" v-if="userPlatform!=null">
          <el-select v-model="form.platform" placeholder="请选择交易所" clearable size="small"
                     @change="changePlatform">
            <el-option
              v-for="dict in userPlatform"
              :key="dict.platform"
              :label="dict.platform"
              :value="dict.platform"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="APIKEY" prop="platform" v-if="userApi!=null">
          <el-select v-model="form.apiId" placeholder="请选择API-KEY" clearable size="small">
            <el-option
              v-for="dict in userApi"
              :key="dict.apiId"
              :label="dict.apiKey"
              :value="dict.apiId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="币对" prop="platform" v-if="userSymbol!=null">
          <el-select v-model="form.symbolName" placeholder="请选择币对" clearable size="small" @change="changeSymbol">
            <el-option
              v-for="dict in userSymbol"
              :key="dict.symbolId"
              :label="dict.name"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标签" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入标签"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listExchange,
    getExchange,
    delExchange,
    addExchange,
    updateExchange,
    exportExchange
  } from "@/api/platform/exchange";
  import {listPlatform} from "@/api/platform/platform";
  import {listSymbol} from "@/api/platform/symbol";
  import {listApi} from "@/api/platform/api";

  export default {
    name: "Exchange",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 运行机器人币对表格数据
        exchangeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        runningInfo: {
          runningId: undefined,
          userId: undefined,
          userName: undefined
        },
        // 交易所字典
        platformOptions: [],

        userPlatform: [],
        userSymbol: [],
        userApi: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          platform: undefined,
          apiId: undefined,
          runningId: undefined,
          symbolId: undefined,
          symbolName: undefined,
          tagName: undefined,
          sequence: undefined
        },
        // 表单参数
        form: {
          runningId: undefined,
          apiId: undefined
        },
        // 表单校验
        rules: {
          platform: [
            {required: true, message: "交易所不能为空", trigger: "blur"}
          ],
          apiId: [
            {required: true, message: "api_id不能为空", trigger: "blur"}
          ],
          symbolId: [
            {required: true, message: "symbol_id不能为空", trigger: "blur"}
          ],
          symbolName: [
            {required: true, message: "币对名称不能为空", trigger: "blur"}
          ],
          tagName: [
            {required: true, message: "标签不能为空", trigger: "blur"}
          ],
          sequence: [
            {required: true, message: "顺序不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.runningInfo.runningId = this.$route.query['runningId']
      this.runningInfo.userId = this.$route.query['userId']
      this.runningInfo.userName = this.$route.query['userName']
      this.queryParams.runningId=this.runningInfo.runningId

      this.getList();

      this.getDicts("plt_platform_type").then(response => {
        this.platformOptions = response.data;
      });
    },
    methods: {
      /** 查询运行机器人币对列表 */
      getList() {
        this.loading = true;
        listExchange(this.queryParams).then(response => {
          this.exchangeList = response.rows;
          this.total = response.total;
        });
        let query = {
          userName: this.runningInfo.userName,
          pageNum: 1,
          pageSize: 100,
        }
        console.log(query)
        listPlatform(query).then(res => {
          this.userPlatform = res.rows
          this.loading = false;
        })
      },
      changePlatform($event) {
        let query = {
          userId: this.runningInfo.userId,
          platform: $event,
          pageNum: 1,
          pageSize: 100,
        }

        listSymbol(query).then(res => {
          this.userSymbol = res.rows
        })
        listApi(query).then(res => {
          this.userApi = res.rows
        })
      },
      changeSymbol($event) {
        this.form.symbolName = $event.name;
        this.form.symbolId = $event.symbolId;
        console.log(this.form)
      },
      // 交易所字典翻译
      platformFormat(row, column) {
        return this.selectDictLabel(this.platformOptions, row.platform);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          runningId: this.runningInfo.runningId,
          platform: undefined,
          apiId: undefined,
          symbolId: undefined,
          symbolName: undefined,
          tagName: undefined,
          sequence: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.runningId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加运行机器人币对";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const runningId = row.runningId || this.ids
        getExchange(runningId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改运行机器人币对";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // if (this.form.runningId != undefined) {
            //   updateExchange(this.form).then(response => {
            //     if (response.code === 200) {
            //       this.msgSuccess("修改成功");
            //       this.open = false;
            //       this.getList();
            //     }
            //   });
            // } else {
            addExchange(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
            // }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const runningIds = row.runningId || this.ids;
        this.$confirm('是否确认删除运行机器人币对编号为"' + runningIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delExchange(runningIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有运行机器人币对数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportExchange(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
